#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse

script_update=2020.03.16
script_version=r10006

################################################################################################ Debug

usage_for_debug() {
    export local_packages=/etc/inexistence/00.Installation
    export s=$local_packages/package/novnc/install
    rm -f $s ; nano $s ; bash $s
    bash /etc/inexistence/00.Installation/package/novnc/install
    bash <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/package/novnc/install)
}

################################################################################################ Get options

unset LogRootPath LogTimes DebLocation SCLocation LOCKLocation OutputLOG

AppName=noVNC
AppNameLower=novnc
Need_SourceCode=yes
pm_action=install

function show_usage() { echo "
$AppName $pm_action $script_version ($script_update)
Usage:
      -l        Specifiy LogBase (Not log file)
      -d        Enable debug mode
      --log     Show logfile's content"
exit 1 ; }

OPTS=$(getopt -o v:l:d -al version:,debug,logbase:,log -- "$@")
[ ! $? = 0 ] && show_usage
eval set -- "$OPTS"

while [ -n "$1" ] ; do case "$1" in
    -v | --version      ) version="$2"  ; shift 2 ;;
    -l | --logbase      ) LogTimes="$2" ; shift 2 ;;
    -d | --debug        ) debug=1       ; shift   ;;
         --log          ) show_log=1    ; shift   ;;
    -- ) shift ; break ;;
esac ; done

################################################################################################ Set Variables

if [[ -f /etc/inexistence/00.Installation/function ]]; then
    source /etc/inexistence/00.Installation/function
else
    source <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/function)
fi

set_variables_log_location
check_var_OutputLOG
debug_log_location
cat_outputlog

################################################################################################




function install_novnc() {
    echo >> $OutputLOG

    if [[ $DISTRO == "Ubuntu" ]]; then
        apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0x0AB215679C571D1C8325275B9BDB3D89CE49EC21 >> "$OutputLOG" 2>&1
        add-apt-repository ppa:mozillateam/ppa -y                              >> "$OutputLOG" 2>&1
    fi

    APT_UPGRADE

    if [[ $CODENAME == bionic ]]; then
        apt_install_check libappindicator-dev
        apt_install_separate
	fi

    apt_install_check   xfce4 xfce4-goodies tightvncserver fonts-noto firefox-esr   expect unzip git python \
                        dbus-x11 xfonts-base xinit xserver-xorg xserver-xorg-legacy x11-xserver-utils iptables-persistent  
    apt_install_separate

    wget -nv -O master.zip 'https://github.com/novnc/noVNC/archive/master.zip' >> "$OutputLOG" 2>&1
    unzip master.zip                                                           >> "$OutputLOG" 2>&1
    rm -rf master.zip                                                          >> "$OutputLOG" 2>&1
    mkdir -p ${web_root}                                                       >> "$OutputLOG" 2>&1
    mv noVNC-master ${web_root}/novnc/                                         >> "$OutputLOG" 2>&1

    cat << EOF > /tmp/openssl.cnf
[ req ]
prompt = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
C = US
ST = Some State
L = LOCALLY
O = SELF
OU = SELF
CN = SELF
emailAddress = private@example.com
EOF
  # echo_info "Generating self-signed certificate for noVNC security..."
    openssl req -config /tmp/openssl.cnf -newkey rsa:1024 -x509 -days 1095 -nodes \
                -out ${web_root}/novnc/utils/self.pem -keyout ${web_root}/novnc/utils/self.pem  >> "$OutputLOG" 2>&1
    chown www-data:www-data -R ${web_root}/novnc/                              >> "$OutputLOG" 2>&1
    chmod +rx ${web_root}/novnc/utils/launch.sh                                >> "$OutputLOG" 2>&1

    status_lock=$AppNameLower
    echo "status_lock=$status_lock" > /tmp/Variables
    rm -f /tmp/$status_lock.1.lock /tmp/$status_lock.2.lock 
    [[ -n $(which vncserver) ]] && [[ -f ${web_root}/novnc/utils/self.pem ]] && touch /tmp/$status_lock.1.lock || touch /tmp/$status_lock.2.lock
    [[ -f /tmp/$status_lock.1.lock ]] && touch $LOCKLocation/$AppNameLower.lock

    echo >> $OutputLOG
}





echo_task "Installing $AppName ..."
install_novnc & spinner $!
check_status $status_lock
